package com.yanqu.road.entity.union;

import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class UnionUpgradeInfo {

	private int level;

	private long experience;

	private int maxMemberCount;

	private int deputyMasterCount;

	private int creamCount;

	private int discount;

	private String qualityPro;

	private int weight;

	private int[] quality;

	private String newMaxPerson;

	private List<UnionMemberCountLimit> unionMemberCountLimitList = new ArrayList<>();

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public long getExperience() {
		return experience;
	}

	public void setExperience(long experience) {
		this.experience = experience;
	}

	public int getMaxMemberCount() {
		return maxMemberCount;
	}

	public void setMaxMemberCount(int maxMemberCount) {
		this.maxMemberCount = maxMemberCount;
	}

	public int getDeputyMasterCount() {
		return deputyMasterCount;
	}

	public void setDeputyMasterCount(int deputyMasterCount) {
		this.deputyMasterCount = deputyMasterCount;
	}

	public int getCreamCount() {
		return creamCount;
	}

	public void setCreamCount(int creamCount) {
		this.creamCount = creamCount;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public void setQualityPro(String qualityPro) {
		this.qualityPro = qualityPro;
		if (StringUtils.isNullOrEmpty(qualityPro)) {
			return;
		}
		weight = 0;
		String[] valueStr = qualityPro.split(";");
		quality = new int[valueStr.length];
		for (int i = 0; i < valueStr.length; i++) {
			int value = Integer.valueOf(valueStr[i]);
			weight += value;
			quality[i] = value;
		}
	}

	public int getIndexByWeight(int qualityValue) {
		if (StringUtils.isNullOrEmpty(qualityPro)) {
			return 0;
		}
		int w = 0;
		for (int i = 0; i < quality.length; i++) {
			w += quality[i];
			if (qualityValue < w) {
				return i;
			}
		}
		return 0;
	}

	public int getWeight() {
		return weight;
	}

	public int getNewMaxPerson(int crossServerCount) {
		for(UnionMemberCountLimit data : unionMemberCountLimitList){
			if(crossServerCount >= data.getCrossServerCount()){
				return data.getMemberCount();
			}
		}
		return maxMemberCount;
	}

	public void setNewMaxPerson(String newMaxPerson) {
		this.newMaxPerson = newMaxPerson;
		unionMemberCountLimitList = parseUnionMemberCountLimitList(newMaxPerson);
	}

	private List<UnionMemberCountLimit> parseUnionMemberCountLimitList(String param){
		List<UnionMemberCountLimit> dataList = new ArrayList<>();
		if(!StringUtils.isNullOrEmpty(param)) {
			String[] paramArr = param.split("\\|");
			for (String dataStr : paramArr) {
				String[] dataArr = dataStr.split(";");
				UnionMemberCountLimit data = new UnionMemberCountLimit();
				data.setCrossServerCount(Integer.parseInt(dataArr[0]));
				data.setMemberCount(Integer.parseInt(dataArr[1]));
				dataList.add(data);
			}
		}
		dataList.sort((o1, o2) -> o2.getCrossServerCount() - o1.getCrossServerCount());
		return dataList;
	}
}
